package com.holly.unit.gen.starter.util;

import com.holly.unit.gen.api.constants.GenFileCfgConstants;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述:
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/1/21 15:27
 */
public class GenFileUtil {

    public static boolean  forceDel(File file) {

        boolean res = false;

        int tryCount = 0;

        while (!res && tryCount++ <10){
            System.gc();
            res = file.delete();
        }

        return res;

    }

    /***
     * 判断是否分割模板
     * @param file
     * @return
     */
    public static boolean isCutFile(File file) {
        if (file.getName().startsWith("[1-n]")) {
            return true;
        }
        return false;
    }

    public static void cutFile(File file,String split,List<String> genFileRes) {
        InputStreamReader isr = null;
        BufferedReader br = null;
        List<Writer> flist = new ArrayList<>();

        try{
            isr = new InputStreamReader(new FileInputStream(file), GenFileCfgConstants.SRC_ENCODE);
            br = new BufferedReader(isr);
            String row;
            boolean start = false;//开始分割标志
            Writer tarFile = null;
            while ((row=br.readLine()) != null) {
                if (row.trim().length()>0 && row.startsWith(split)) {
                    String fileName = row.substring(split.length());
                    String parent = file.getParentFile().getAbsolutePath();
                    fileName = parent+File.separator+fileName;
                    tarFile = new OutputStreamWriter(new FileOutputStream(fileName),GenFileCfgConstants.SRC_ENCODE);
                    flist.add(tarFile);
                    genFileRes.add("生成成功:"+fileName);
                    start = true;
                } else if (start) {
                    tarFile.append(row+"\r\n");
                }
            }

            for (int i=0;i<flist.size();i++) {
                flist.get(i).close();
            }
            br.close();
            isr.close();

            forceDel(file);

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{
                if (br != null) {
                    br.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (flist.size() > 0) {
                    for (int i=0;i<flist.size();i++) {
                        if (flist.get(i) != null) {
                            flist.get(i).close();
                        }
                    }
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public static File createParentDir(String file) {
        if (file == null) throw new IllegalArgumentException("file not null");
        File res = new File(file);
        createParentDir(res);
        return res;
    }

    public static void createParentDir(File out){
        if (out.getParentFile() != null) {
            out.getParentFile().mkdirs();
        }
    }

}
